package com.fw.ui.view.profile.service.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.formvalidator.FormValidatorService;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.view.event.AbstractListener;
import com.fw.ui.view.profile.CreateCashServiceProfileView;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.validator.RegexpValidator;

@Component
@Scope(SCOPE_PROTOTYPE)
public class CreateCashServiceProfileIsBankGuaranteeValueChangeListener extends AbstractListener implements ValueChangeListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    private MessageBAO messageBAO;

    @Override
    public void valueChange(ValueChangeEvent event) {
	final CreateCashServiceProfileView createCashServiceProfileView = (CreateCashServiceProfileView) this.getBasedComponent();
	Boolean selected = (Boolean) event.getProperty().getValue();
	if (selected) {
	    createCashServiceProfileView.getTxtBankGuarantee().setImmediate(true);
	    createCashServiceProfileView.getTxtBankGuarantee().setReadOnly(false);
	    createCashServiceProfileView.getTxtBankGuarantee().removeAllValidators();
	    createCashServiceProfileView.getTxtBankGuarantee().setRequired(true);
	    createCashServiceProfileView.getTxtBankGuarantee().setRequiredError(
		    this.messageBAO.getMessage("com.fw.ui.view.profile.CreateCashServiceProfileView.message.bankGuarantee.mandatory"));
	    RegexpValidator cancelUrlRegexpValidator = new RegexpValidator(FormValidatorService.PATTERN_NUMBER, true,
		    this.messageBAO.getMessage("com.fw.ui.view.profile.CreateCashServiceProfileView.message.bankGuarantee.format"));
	    createCashServiceProfileView.getTxtBankGuarantee().addValidator(cancelUrlRegexpValidator);
	    createCashServiceProfileView.getTxtBankGuarantee().setValidationVisible(true);

	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setImmediate(true);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setReadOnly(false);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().removeAllValidators();
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setRequired(true);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setRequiredError(
		    this.messageBAO
			    .getMessage("com.fw.ui.view.profile.CreateCashServiceProfileView.message.bankGuaranteeExpireDate.mandatory"));
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setValidationVisible(true);

	} else {
	    createCashServiceProfileView.getTxtBankGuarantee().setImmediate(true);
	    createCashServiceProfileView.getTxtBankGuarantee().setComponentError(null);
	    createCashServiceProfileView.getTxtBankGuarantee().setValidationVisible(false);
	    createCashServiceProfileView.getTxtBankGuarantee().setValue("");
	    createCashServiceProfileView.getTxtBankGuarantee().setRequired(false);
	    createCashServiceProfileView.getTxtBankGuarantee().setRequiredError("");
	    createCashServiceProfileView.getTxtBankGuarantee().removeAllValidators();
	    createCashServiceProfileView.getTxtBankGuarantee().setReadOnly(true);
	    createCashServiceProfileView.getTxtBankGuarantee().removeStyleName("v-textfield-error");
	    createCashServiceProfileView.getLblBankGuaranteeValidateMessage().setValue("");
	    createCashServiceProfileView.getLayoutBankGuaranteeValidate().setVisible(false);

	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setImmediate(true);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setComponentError(null);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setValidationVisible(false);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setValue(null);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setRequired(false);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setRequiredError("");
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().removeAllValidators();
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().setReadOnly(true);
	    createCashServiceProfileView.getPdfBankGuaranteeExpireDate().removeStyleName("v-textfield-error");
	    createCashServiceProfileView.getLblBankGuaranteeExpireDateValidateMessage().setValue("");
	    createCashServiceProfileView.getLayoutBankGuaranteeExpireDateValidate().setVisible(false);

	}
    }
}
